/**
 * Copyright (c) 2019 xstgongyi.org
 * All rights reserved.
 */
package org.xstgongyi.eduaid.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xstgongyi.eduaid.bo.*;
import org.xstgongyi.eduaid.common.ControllerHelper;
import org.xstgongyi.eduaid.dataobject.*;
import org.xstgongyi.eduaid.manager.AccountingSubjectManager;
import org.xstgongyi.eduaid.manager.BankAccountManager;
import org.xstgongyi.eduaid.service.GradeService;
import org.xstgongyi.eduaid.service.ProjectService;
import org.xstgongyi.eduaid.service.SchoolYearService;
import org.xstgongyi.eduaid.service.SemesterService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 提供可公开访问的主数据信息（列表、元素）,一般情况下，列表只包含状态为active的元素。
 *
 * @author oldcrane<br   />
 * @created 2019-04-02
 */
@Controller
@RequestMapping("/public")
public class PublicDataController {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private SemesterService semesterService;

    @Autowired
    private SchoolYearService schoolYearService;

    @Autowired
    private GradeService gradeService;

    @Autowired
    private AccountingSubjectManager accountingSubjectManager;

    @Autowired
    private BankAccountManager bankAccountManager;

    private static Map<String, String> genOption(String code, String text) {
        Map<String, String> option = new HashMap<>();
        option.put("code", code);
        option.put("name", text);
        return option;
    }

    @GetMapping("/school-years")
    @ResponseBody
    public Map<String, Object> listActiveSchoolYears() {
        List<SchoolYearDO> schoolYears = schoolYearService.listByStatus(true);
        List<Map<String, String>> options = new ArrayList<>();
        for (SchoolYearDO schoolYear : schoolYears) {
            options.add(genOption(schoolYear.getId().toString(), schoolYear.getName()));
        }
        return ControllerHelper.successResponse(options);
    }

    @GetMapping("/semesters")
    @ResponseBody
    public Map<String, Object> listActiveSemesters() {
        List<SemesterDO> semesters = semesterService.listByStatus(true);
        List<Map<String, String>> options = new ArrayList<>();
        for (SemesterDO semester : semesters) {
            options.add(genOption(semester.getId().toString(), semester.getName()));
        }
        return ControllerHelper.successResponse(options);
    }

    @GetMapping("/education-stages")
    @ResponseBody
    public Map<String, Object> listEducationStages() {
        List<Map<String, String>> options = new ArrayList<>();
        for (EducationStage stage : EducationStage.values()) {
            options.add(genOption(stage.name(), stage.getLabel()));
        }
        return ControllerHelper.successResponse(options);
    }

    @GetMapping("/grades")
    @ResponseBody
    public Map<String, Object> listActiveGrades() {
        List<GradeDO> grades = gradeService.listByStatus(true);
        List<Map<String, String>> options = new ArrayList<>();
        for (GradeDO grade : grades) {
            options.add(genOption(grade.getId().toString(), grade.getName()));
        }
        return ControllerHelper.successResponse(options);
    }

    @GetMapping("/genders")
    @ResponseBody
    public Map<String, Object> listGenders() {
        List<Map<String, String>> options = new ArrayList<>();
        for (Gender gender : Gender.values()) {
            options.add(genOption(gender.name(), gender.getLabel()));
        }
        return ControllerHelper.successResponse(options);
    }

    @GetMapping("/poverty-levels")
    @ResponseBody
    public Map<String, Object> listPovertyLevels() {
        List<Map<String, String>> options = new ArrayList<>();
        for (PovertyLevel level : PovertyLevel.values()) {
            options.add(genOption(level.name(), level.getLabel()));
        }
        return ControllerHelper.successResponse(options);
    }

    @GetMapping("/projects")
    @ResponseBody
    public Map<String, Object> listProjects() {
        // TODO 只获取active的project
        List<ProjectDO> projects = projectService.listAll();
        List<Map<String, String>> options = new ArrayList<>();
        for (ProjectDO project : projects) {
            options.add(genOption(project.getId().toString(), project.getName()));
        }
        return ControllerHelper.successResponse(options);
    }

    @GetMapping("/project-phase-statuses")
    @ResponseBody
    public Map<String, Object> listProjectPhaseStatus() {
        List<Map<String, String>> options = new ArrayList<>();
        for (ProjectPhaseStatus status : ProjectPhaseStatus.values()) {
            options.add(genOption(status.name(), status.getLabel()));
        }
        return ControllerHelper.successResponse(options);
    }

    @GetMapping("/sponsor-types")
    @ResponseBody
    public Map<String, Object> listSponsorTypes() {
        List<Map<String, String>> options = new ArrayList<>();
        for (SponsorType type : SponsorType.values()) {
            options.add(genOption(type.name(), type.getLabel()));
        }
        return ControllerHelper.successResponse(options);
    }

    @GetMapping("/student-aid-statuses")
    @ResponseBody
    public Map<String, Object> listStudentAidStatus() {
        List<Map<String, String>> options = new ArrayList<>();
        for (StudentAidStatus status : StudentAidStatus.values()) {
            options.add(genOption(status.name(), status.getLabel()));
        }
        return ControllerHelper.successResponse(options);
    }

    @GetMapping("/student-current-aid-statuses")
    @ResponseBody
    public Map<String, Object> listStudentCurrentAidStatuses() {
        List<Map<String, String>> options = new ArrayList<>();
        for (StudentCurrentAidStatus status : StudentCurrentAidStatus.values()) {
            options.add(genOption(status.name(), status.getLabel()));
        }
        return ControllerHelper.successResponse(options);
    }

    @GetMapping("/student-aid-types")
    @ResponseBody
    public Map<String, Object> listStudentAidTypes() {
        List<Map<String, String>> options = new ArrayList<>();
        for (StudentAidType type : StudentAidType.values()) {
            options.add(genOption(type.name(), type.getLabel()));
        }
        return ControllerHelper.successResponse(options);
    }

    @GetMapping("/student-pair-statuses")
    @ResponseBody
    public Map<String, Object> listStudentPairStatus() {
        List<Map<String, String>> options = new ArrayList<>();
        for (StudentPairStatus status : StudentPairStatus.values()) {
            options.add(genOption(status.name(), status.getLabel()));
        }
        return ControllerHelper.successResponse(options);
    }

    @GetMapping("/fund-statuses")
    @ResponseBody
    public Map<String, Object> listFundStatus() {
        List<Map<String, String>> options = new ArrayList<>();
        for (FundStatus status : FundStatus.values()) {
            options.add(genOption(status.name(), status.getLabel()));
        }
        return ControllerHelper.successResponse(options);
    }

    @GetMapping("/student-feedback-statuses")
    @ResponseBody
    public Map<String, Object> listStudentFeedbackStatus() {
        List<Map<String, String>> options = new ArrayList<>();
        for (StudentFeedbackStatus status : StudentFeedbackStatus.values()) {
            options.add(genOption(status.name(), status.getLabel()));
        }
        return ControllerHelper.successResponse(options);
    }

    @GetMapping("/sponsor-feedback-statuses")
    @ResponseBody
    public Map<String, Object> listSponsorFeedbackStatus() {
        List<Map<String, String>> options = new ArrayList<>();
        for (SponsorFeedbackStatus status : SponsorFeedbackStatus.values()) {
            options.add(genOption(status.name(), status.getLabel()));
        }
        return ControllerHelper.successResponse(options);
    }

    @GetMapping("/accounting-subjects")
    @ResponseBody
    public Map<String, Object> listAccountingSubjects() {
        // TODO 只获取active的
        List<AccountingSubjectDO> list = accountingSubjectManager.listAll();
        List<Map<String, String>> options = new ArrayList<>();
        for (AccountingSubjectDO item : list) {
            options.add(genOption(item.getId().toString(), item.getName()));
        }
        return ControllerHelper.successResponse(options);
    }

    @GetMapping("/donate-statuses")
    @ResponseBody
    public Map<String, Object> listDonateStatus() {
        List<Map<String, String>> options = new ArrayList<>();
        for (DonateStatus status : DonateStatus.values()) {
            options.add(genOption(status.name(), status.getLabel()));
        }
        return ControllerHelper.successResponse(options);
    }

}
